/* linkit.js
 * By François Leporcq
 * MIT Licensed.
 */
var Label = Composite.extend({
    _className: "Label",
    init: function(content, origin, padding){
        this._origin = origin;
        this._text = new Text(this._origin, content);
        this._rectangle = new Rectangle(this._origin);
        this._width = 0;
        this._height = 0;
        this._padding = (padding != null) ? padding : Workspace.label.padding;
        this.setShapes(this.getShapes());
    },
    setOrigin: function(origin){
        this._origin = origin;
        this._rectangle.setOrigin(origin)
        this._setTextOrigin();
    },
    getShapes: function(){
        return [this._rectangle,this._text];
    },
    setWidth: function(width){
        this._width = width;
        this._rectangle.setWidth(this._width);
        this._setTextOrigin();
    },
    getWidth: function(){
        return this._width;
    },
    setHeight: function(height){
        this._height = height;
        this._rectangle.setHeight(this._height);
        this._setTextOrigin();
    },
    _setTextOrigin: function(){
        var x = this._origin.getX() + this._width / 2 ;
        var y = this._origin.getY() + this._text.getComputedHeight() / 2 + this._height / 2 - this._text.getComputedHeight() / 2;

        this._text.setOrigin(new Point(x,y));
    },
    getHeight: function(){
        return this._height;
    },
    afterWorkspaceAdd: function(workspace){
       this._ninjaComputeDimension(workspace);
       this._text.afterWorkspaceAdd(workspace);
    },
    _ninjaComputeDimension: function(workspace){
        var oldStatus = this.isPartOfComposite();
        this.setPartOfComposite(false);
        workspace.draw(this);
        var box = this._text.element.getBBox();
        workspace.reset(this);
        this.setPartOfComposite(oldStatus);
        this.setWidth(box.width + this._padding * 2);
        this.setHeight(box.height + this._padding * 2);

    },
    getCenter:function(){
        this._rectangle.getCenter();
        return this._rectangle.getCenter();
    },
    getAnchor:function(point){
        return this._rectangle.getAnchor(point);
    },
    getBox: function(){
        return this._rectangle.getBox();
    }


}).implement([Linkable]);